import Vue from 'vue'
import App from './App'

import basics from './pages/basics/home.vue'
Vue.component('basics', basics)

import components from './pages/component/home.vue'
Vue.component('components', components)

import plugin from './pages/plugin/home.vue'
Vue.component('plugin', plugin)

import cuCustom from './colorui/components/cu-custom.vue'
Vue.component('cu-custom', cuCustom)

Vue.config.productionTip = false

App.mpType = 'app'


//登录start
//来自 graceUI 的表单验证， 使用说明见手册 http://grace.hcoder.net/doc/info/73-3.html
import dataCheck from '@/common/graceChecker.js'
Vue.prototype.dataCheck = dataCheck

import store from './store'
Vue.prototype.$store = store
import request from '@/common/request.js'

//自定义标签防重复点击
Vue.directive('noMoreClick', {
	inserted(el, binding) {
		el.addEventListener('click', e => {
			el.classList.add('uni-tag--disabled');
			el.disabled = true;
			setTimeout(() => {
				el.disabled = false;
				el.classList.remove('uni-tag--disabled');
			}, 2000)
		})
	}
});
// 全局配置
request.setConfig({
	baseUrl: store.state.baseUrl, // 此为测试地址，需加入到域名白名单，或者更改为您自己的线上地址即可
	dataType: 'json', // 可删除，默认为json
	responseType: 'text', // 可删除，默认为text
	// 设置请求头，支持所有请求头设置，也可不设置，去掉header就行
	header: {
		'token': 'BearereyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0IiwianRpIjoiMSIsImlhdCI6MTU2MDY2MjkyNCwiZXhwIjoxNTYxNTI2OTI0fQ.QVFczydsiNoLK8WKBvOvWzj8xe_Hv8gXlg46B8QYOcA',
		'content-type': 'application/json'
	}
});
import md5 from 'js-md5';
// 设置请求拦截器
request.interceptors.request(config => {
	// 配置参数和全局配置相同，此优先级最高，会覆盖在其他地方的相同配置参数

	// 追加请求头，推荐
	let jsonStr = '{}';
	if (config.data != undefined) {
		jsonStr = JSON.stringify(config.data);
	}
	console.log(jsonStr + '/' + store.state.SLAT)
	config.header['sign'] = md5(jsonStr + '/' + store.state.SLAT);
	//config.header.token = 'token from interceptors';

	// 覆盖请求头
	// config.header = {
	// 'content-type': 'application/json',
	// 'token': 'token from interceptors'
	// }

	// return false; // 终止请求
	// return Promise.reject('error from request interceptors'); // 向外层抛出错误，用catch捕获
	return config; // 返回修改后的配置，如未修改也需添加这行
});

// 设置响应拦截器
request.interceptors.response(res => {
	if (res.statusCode == 203) {
		uni.showModal({
			title: '203',
			content: '请求加密异常',
			showCancel: false
		});
		return Promise.reject(res.errMsg);
	}
	if (res.statusCode == 401) {
		uni.showModal({
			title: '401',
			content: '登录校验异常请重新登录',
			showCancel: false
		});
		uni.navigateTo({
			url: '/pages/login/login',
		});
		return Promise.reject(res.errMsg);
	}
	if (res.statusCode != 200) {
		uni.showModal({
			title: res.statusCode,
			content: res.errMsg,
			showCancel: false
		});
		return Promise.reject(res.errMsg);
	}
	if (res.data.code != '200') {
		uni.showModal({
			title: '服务异常',
			content: res.data.msg,
			showCancel: false
		});
		return Promise.reject(res.data.msg);
	}
	return res.data; // 原样返回
});

// // 挂载到全局vue实例上，在页面中可以使用this.$request调用request实例下相应方法
Vue.prototype.$request = request;
//登录 end

const app = new Vue({
	store,
	...App
})
app.$mount()
